package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.GroupDao;
import com.gdit.zax.dao.StartPicDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.GroupTeacher;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.webUtil;

public class AdmClass extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int page = TextUtil.String2int(request.getParameter("page"), 1);
		int onepagecount = TextUtil.String2int(request.getParameter("onepagecount"), 10);
		
		//数据库、登陆检测
		Connection conn = DBUtil.getConnection();
		if (conn == null) {
			Repost.dberror(request, response);
			return;
		}
		
		String token = webUtil.getCookie(request, "LoginCookie");
		
		DAOFactory dao = DAOFactory.init(conn, request, response);
		UserDao udao = dao.getUserDao();
		UserInfo info = udao.getInfo(Constant.LOGINTYPE_WEB, token);
		if (info==null) {
			Repost.nologin(response);
			return;
		}
		if (info.getUlevel()<Constant.ULEVEL_teacher) {
			Repost.message("权限不够!", request, response);
			return;
		}
		
		//开始
		GroupDao gdao = dao.getGroupDao();
		List<GroupTeacher> list = gdao.listAllGroupInfo(page, onepagecount);
		LogUtil.print("list:" + list.size());
		for (GroupTeacher groupTeacher : list) {
			LogUtil.print(groupTeacher.getT_group_name());
		}
		request.setAttribute("list",list );
		Repost.success("admclass.jsp", request, response);
		
	}

}
